home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_20 / mftext.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  5KB  |  271 lines

  1. /*
  2.  * mftext
  3.  * 
  4.  * Convert a MIDI file to verbose text.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "midifile.h"
  10.  
  11. static FILE *F;
  12. int SECONDS;      /* global that tells whether to display seconds or ticks */
  13. int division;        /* from the file header */
  14. long tempo = 500000; /* the default tempo is 120 beats/minute */
  15.  
  16. filegetc()
  17. {
  18.     return(getc(F));
  19. }
  20.  
  21. /* for crack */
  22. extern int arg_index;
  23.  
  24. main(argc,argv)
  25. char **argv;
  26. {
  27.     FILE *efopen();
  28.     char ch;
  29.  
  30.     SECONDS = 0;
  31.  
  32.     while((ch = crack(argc,argv,"s",0)) != NULL){
  33.         switch(ch){
  34.         case 's' : SECONDS = 1; break;
  35.         }
  36.         }
  37.  
  38.     if ( argc < 2 && !SECONDS || argc < 3 && SECONDS )
  39.         F = stdin;
  40.     else
  41.         F = efopen(argv[arg_index],"r");
  42.  
  43.     initfuncs();
  44.     Mf_getc = filegetc;
  45.     midifile();
  46.     fclose(F);
  47.     exit(0);
  48. }
  49.  
  50. FILE *
  51. efopen(name,mode)
  52. char *name;
  53. char *mode;
  54. {
  55.     FILE *f;
  56.     extern int errno;
  57.     extern char *sys_errlist[];
  58.     extern int sys_nerr;
  59.     char *errmess;
  60.  
  61.     if ( (f=fopen(name,mode)) == NULL ) {
  62.         (void) fprintf(stderr,"*** ERROR *** Cannot open '%s'!\n",name);
  63.         if ( errno <= sys_nerr )
  64.             errmess = sys_errlist[errno];
  65.         else
  66.             errmess = "Unknown error!";
  67.         (void) fprintf(stderr,"************* Reason: %s\n",errmess);
  68.         exit(1);
  69.     }
  70.     return(f);
  71. }
  72.  
  73. error(s)
  74. char *s;
  75. {
  76.     fprintf(stderr,"Error: %s\n",s);
  77. }
  78.  
  79. txt_header(format,ntrks,ldivision)
  80. {
  81.         division = ldivision; 
  82.     printf("Header format=%d ntrks=%d division=%d\n",format,ntrks,division);
  83. }
  84.  
  85. txt_trackstart()
  86. {
  87.     printf("Track start\n");
  88. }
  89.  
  90. txt_trackend()
  91. {
  92.     printf("Track end\n");
  93. }
  94.  
  95. txt_noteon(chan,pitch,vol)
  96. {
  97.     prtime();
  98.     printf("Note on, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
  99. }
  100.  
  101. txt_noteoff(chan,pitch,vol)
  102. {
  103.     prtime();
  104.     printf("Note off, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
  105. }
  106.  
  107. txt_pressure(chan,pitch,press)
  108. {
  109.     prtime();
  110.     printf("Pressure, chan=%d pitch=%d press=%d\n",chan+1,pitch,press);
  111. }
  112.  
  113. txt_parameter(chan,control,value)
  114. {
  115.     prtime();
  116.     printf("Parameter, chan=%d c1=%d c2=%d\n",chan+1,control,value);
  117. }
  118.  
  119. txt_pitchbend(chan,msb,lsb)
  120. {
  121.     prtime();
  122.     printf("Pitchbend, chan=%d msb=%d lsb=%d\n",chan+1,msb,lsb);
  123. }
  124.  
  125. txt_program(chan,program)
  126. {
  127.     prtime();
  128.     printf("Program, chan=%d program=%d\n",chan+1,program);
  129. }
  130.  
  131. txt_chanpressure(chan,press)
  132. {
  133.     prtime();
  134.     printf("Channel pressure, chan=%d pressure=%d\n",chan+1,press);
  135. }
  136.  
  137. txt_sysex(leng,mess)
  138. char *mess;
  139. {
  140.     prtime();
  141.     printf("Sysex, leng=%d\n",leng);
  142. }
  143.  
  144. txt_metamisc(type,leng,mess)
  145. char *mess;
  146. {
  147.     prtime();
  148.     printf("Meta event, unrecognized, type=0x%02x leng=%d\n",type,leng);
  149. }
  150.  
  151. txt_metaspecial(type,leng,mess)
  152. char *mess;
  153. {
  154.     prtime();
  155.     printf("Meta event, sequencer-specific, type=0x%02x leng=%d\n",type,leng);
  156. }
  157.  
  158. txt_metatext(type,leng,mess)
  159. char *mess;
  160. {
  161.     static char *ttype[] = {
  162.         NULL,
  163.         "Text Event",        /* type=0x01 */
  164.         "Copyright Notice",    /* type=0x02 */
  165.         "Sequence/Track Name",
  166.         "Instrument Name",    /* ...       */
  167.         "Lyric",
  168.         "Marker",
  169.         "Cue Point",        /* type=0x07 */
  170.         "Unrecognized"
  171.     };
  172.     int unrecognized = (sizeof(ttype)/sizeof(char *)) - 1;
  173.     register int n, c;
  174.     register char *p = mess;
  175.  
  176.     if ( type < 1 || type > unrecognized )
  177.         type = unrecognized;
  178.     prtime();
  179.     printf("Meta Text, type=0x%02x (%s)  leng=%d\n",type,ttype[type],leng);
  180.     printf("     Text = <");
  181.     for ( n=0; n<leng; n++ ) {
  182.         c = *p++;
  183.         printf( (isprint(c)||isspace(c)) ? "%c" : "\\0x%02x" , c);
  184.     }
  185.     printf(">\n");
  186. }
  187.  
  188. txt_metaseq(num)
  189. {
  190.     prtime();
  191.     printf("Meta event, sequence number = %d\n",num);
  192. }
  193.  
  194. txt_metaeot()
  195. {
  196.     prtime();
  197.     printf("Meta event, end of track\n");
  198. }
  199.  
  200. txt_keysig(sf,mi)
  201. {
  202.     prtime();
  203.     printf("Key signature, sharp/flats=%d  minor=%d\n",sf,mi);
  204. }
  205.  
  206. txt_tempo(ltempo)
  207. long ltempo;
  208. {
  209.     tempo = ltempo;
  210.     prtime();
  211.     printf("Tempo, microseconds-per-MIDI-quarter-note=%d\n",tempo);
  212. }
  213.  
  214. txt_timesig(nn,dd,cc,bb)
  215. {
  216.     int denom = 1;
  217.     while ( dd-- > 0 )
  218.         denom *= 2;
  219.     prtime();
  220.     printf("Time signature=%d/%d  MIDI-clocks/click=%d  32nd-notes/24-MIDI-clocks=%d\n",
  221.         nn,denom,cc,bb);
  222. }
  223.  
  224. txt_smpte(hr,mn,se,fr,ff)
  225. {
  226.     prtime();
  227.     printf("SMPTE, hour=%d minute=%d second=%d frame=%d fract-frame=%d\n",
  228.         hr,mn,se,fr,ff);
  229. }
  230.  
  231. txt_arbitrary(leng,mess)
  232. char *mess;
  233. {
  234.     prtime();
  235.     printf("Arbitrary bytes, leng=%d\n",leng);
  236. }
  237.  
  238. prtime()
  239. {
  240.     if(SECONDS)
  241.     printf("Time=%f   ",mf_ticks2sec(Mf_currtime,division,tempo));
  242.     else
  243.     printf("Time=%ld  ",Mf_currtime);
  244. }
  245.  
  246. initfuncs()
  247. {
  248.     Mf_error = error;
  249.     Mf_header =  txt_header;
  250.     Mf_trackstart =  txt_trackstart;
  251.     Mf_trackend =  txt_trackend;
  252.     Mf_noteon =  txt_noteon;
  253.     Mf_noteoff =  txt_noteoff;
  254.     Mf_pressure =  txt_pressure;
  255.     Mf_parameter =  txt_parameter;
  256.     Mf_pitchbend =  txt_pitchbend;
  257.     Mf_program =  txt_program;
  258.     Mf_chanpressure =  txt_chanpressure;
  259.     Mf_sysex =  txt_sysex;
  260.     Mf_metamisc =  txt_metamisc;
  261.     Mf_seqnum =  txt_metaseq;
  262.     Mf_eot =  txt_metaeot;
  263.     Mf_timesig =  txt_timesig;
  264.     Mf_smpte =  txt_smpte;
  265.     Mf_tempo =  txt_tempo;
  266.     Mf_keysig =  txt_keysig;
  267.     Mf_seqspecific =  txt_metaspecial;
  268.     Mf_text =  txt_metatext;
  269.     Mf_arbitrary =  txt_arbitrary;
  270. }
  271.